#!/bin/bash

#Desc:source install mysql 5.7 
#author:huan
#update:8-24
#usage:$1 sourcedir $2 port $3 install dir $4 -jn

packagepath=`pwd`
sourcedir="mysqlsource"

test(){
if [ ! -f $1 ]
then
	echo "请输入正确的压缩包的路径！！！"
fi

echo "$3" |egrep "[ ]|[^\/]"
if [ $? -eq 0 ]
then
	echo "请正确的输入安装目录的绝对路径(路径中不能有空格)"
fi
echo $3 | egrep "^(\/.*)|(\/) "

if [ $? -gt 0 ]
then
	echo "请正确的输入安装目录的绝对路径"
else
	temp=$3
	temp=${temp##*/}
	if [ -z $temp ]
	then
		 echo "//////" |egrep "[^\/]"
	fi
fi
}

install_rely(){
	yum -y install ncurses-devel cmake libaio-devel openssl-devel
}

unpack(){
	mkdir -p $sourcedir
	tar -xf $1 -C  $sourcedir 
	for i in `ls $sourcedir`
	do
		sourcedir="$sourcedir/$i"
	done
}

#$1 install dir $2 port
confcmake(){
	cd "$packagepath/$sourcedir/"
	pwd
	cat > myconfig.sh <<EOF
cmake . \
-DCMAKE_INSTALL_PREFIX=$1 \
-DMYSQL_DATADIR=$1/data \
-DMYSQL_TCP_PORT=$2 \
-DMYSQL_UNIX_ADDR=$1/mysql.sock \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_SSL=system \
-DWITH_BOOST=boost
EOF
		
}
cmake_conf(){
	chmod +x myconfig.sh
	./myconfig.sh
}
#$1 -j2
cmake_make_install(){
	make $1 && make install
}
 
cleanfile(){  
	if [ -d $packagepath/$sourcedir ]
	then
		rm -rf "$packagepath/$sourcedir/"
		rm -rf $1
	fi 
}

main(){
while : 
do
	clear
	cat << EOF
	安装选项
	1)安装依赖
	2)配置cmake
	3)编译安装
	9)完整安装
	q|Q)退出
EOF
	read -p "输入选择项[1|2|3|9|q|Q]:" choise
	echo $choise | egrep "[^1239qQ]" &>/dev/null
	if [ $? -eq 0 ]; then
		echo "请输入正确的选项"
		sleep 2
		continue
	fi
	echo $choise |egrep "[Qq]" &>/dev/null
	if [ $? -eq 0 ]; then
		 exit
	fi
	case $choise in
		1)
			clear
			install_rely
			;;
		2)
			clear
			unpack $1
			confcmake $3 $2
			;;
		3)
			clear
			cmake_conf
			cmake_make_install $4
			;;
		9)
			clear
			install_rely
			unpack $1
			confcmake $3 $2
			cmake_conf
			cmake_make_install $4
			;;
		q|Q)
			exit 0
			;;
	esac
done

}


main $1 $2 $3 $4

































